package org.netty.mvc.http;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.stream.ChunkedWriteHandler;

public class ServerInitializer extends ChannelInitializer<SocketChannel> {

    private final SslContext sslCtx;

    public ServerInitializer(SslContext sslCtx) {
        this.sslCtx = sslCtx;
    }

    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        //https才有的操作
        if (sslCtx != null) {
            pipeline.addLast(sslCtx.newHandler(ch.alloc()));
        }
        //http 服务的解码器
        pipeline.addLast(new HttpServerCodec());
        //http 消息的合并处理
        pipeline.addLast(new HttpObjectAggregator(512 * 1024));
        //用来处理大数据流
        pipeline.addLast(new ChunkedWriteHandler());
        //http 请求header，自己写的服务器逻辑处理
        pipeline.addLast(new HealthServerHandler());
    }
}
